package com.example.demo.leetcode.classics150;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _64删除排序链表中的重复元素II
 * @description
 * @date 2023/07/29 9:33
 * <p>
 * ******************************************************
 */
public class _64删除排序链表中的重复元素II {

    public ListNode deleteDuplicates(ListNode head)  {
        ListNode dummy = new ListNode(-101);
        dummy.next = head;
        ListNode slow = dummy;
        ListNode fast = head;

        while (fast != null && fast.next != null) {
            if (fast.val == fast.next.val) {
                fast = fast.next;
            } else {
                if (slow.next != fast) {
                    fast = fast.next;
                    if (fast.next != null && fast.val == fast.next.val) {
                        fast = fast.next;
                    }else {
                        slow.next = fast;
                        slow = slow.next;
                    }
                } else {
                    slow = slow.next;
                }

            }
        }
        if (head != null && head.next != null)
            slow.next = null;
        return dummy.next;
    }


    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
